package com.example.service.impl;

import cn.hutool.core.date.DateUtil;
import com.example.dao.IDepartmentDao;
import com.example.entity.AreaTime.TimeAreaBO;
import com.example.service.ITimeAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ChenXiaoBin
 * on 2019-07-09
 */
@Service
public class TimeAreaServiceImpl implements ITimeAreaService {

    @Autowired
    private IDepartmentDao departmentDao;


    @Override
    public void curve() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //当前月最后一天
        calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        String lastday = format.format(calendar.getTime());
        Date endDay = null;
        try {
            endDay = format.parse(lastday);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        //获取本月的第一天
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String firstday = format.format(calendar.getTime());
        Date beginDay = null;
        try {
            beginDay = format.parse(firstday);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        TimeAreaBO timeAreaBO = getDate(2);
        String sqlExl = generateSqlData(beginDay, endDay);

        Map map = new HashMap(5);
        map.put("sqlExl", sqlExl);
        departmentDao.getValue(map);
    }


    /**
     * 获取时间区间-时间区间类型 1昨日 2近7日 3近30日
     *
     * @param dateRange
     * @return
     */
    @Override
    public TimeAreaBO getDate(Integer dateRange) {
        TimeAreaBO timeAreaBO = new TimeAreaBO();
        switch (dateRange) {
            case 1:
                timeAreaBO.setStartTime(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -1)));
                timeAreaBO.setEndTime(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), -1)));
                break;
            case 2:
                timeAreaBO.setStartTime(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -6)));
                timeAreaBO.setEndTime(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), 0)));
                break;
            case 3:
                timeAreaBO.setStartTime(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -30)));
                timeAreaBO.setEndTime(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), -1)));
            default:
                break;
        }
        return timeAreaBO;
    }



    private String generateSqlData(Date startDate, Date endDate) {
        StringBuilder sql = new StringBuilder();
//        int length = DateWrap.getDayGapsBetweenDates(
//                DateWrap.format(endDate, "yyyy-MM-dd"), DateWrap.format(startDate, "yyyy-MM-dd"));

        int length = getDayGapsBetweenDates(format(endDate, "yyyy-MM-dd"), format(startDate, "yyyy-MM-dd"), "yyyy-MM-dd");
        for (int i = 0; i <= length; i++) {
            Date current = DateUtil.offsetDay(endDate, i - length);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
            String currentFormat = simpleDateFormat.format(current);

            SimpleDateFormat newsim = new SimpleDateFormat("yyyy-MM-dd");
            String newcurr = newsim.format(current);
//            sql.append(" UNION (SELECT '").append(currentFormat).append("', 0)");
            sql.append("UNION (SELECT '").append(currentFormat).append("', 0").append(", date_format('").append(newcurr).append("', '%w'))");
        }
        return sql.toString();
    }

    public static String format(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    public static int getDayGapsBetweenDates(String dateStr1, String dateStr2, String format) {
        Date date1 = parse(dateStr1, format);
        Date date2 = parse(dateStr2, format);

        assert date1 != null;

        assert date2 != null;

        long l = date1.getTime() - date2.getTime();
        return (int)(l / 86400000L);
    }

    public static Date parse(String dateStr, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

        try {
            return simpleDateFormat.parse(dateStr);
        } catch (ParseException var4) {
            var4.printStackTrace();
            return null;
        }
    }
}
